package com.dhcc.httpserver;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

import com.dhcc.util.CloseUtil;




public class Server {
	
	private ServerSocket server;
	
	private boolean isShutdown = false;
	
	

	public static void main(String[] args) {
		
		Server server = new Server();
		server.start();
		
	}
	
	
	/**
	 * @param part 端口
	 */
	public void start(int part){
		
		try {
			server = new ServerSocket(part);
			this.receive();
		} catch (IOException e) {
			stop();
		}
		
	}
	
	/**
	 * 
	 * 启动方法 8888
	 */
	public void start(){
		
		start(8888);
		
	}
	

	/**
	 * 接收客户端
	 */
	@SuppressWarnings("unused")
	private void receive(){
		
		try {
			
			while(!isShutdown){
				Socket client = server.accept();
				//请求
				new Thread(new Dispatcher(client)).start();
			}
		} catch (IOException e) {
			
			stop();
		}
		
		
		
	}
	
	/**
	 * 停止服务器
	 */
	public void stop(){
		isShutdown = true;
		CloseUtil.closeAll(server);
		
		
	}
	

}
